// 登录方法

import request from "@/utils/request.js";
import {clearAllStore, setStore} from "@/utils/util.js";
import router, {handleNestedRoutes} from "@/router/index.js";
import {ElMessage} from "element-plus";

export class LoginService {
    static login(data) {
        return request({
            url: '/api/login',
            method: 'post',
            data: data
        })
    }

    static async getInfo() {
        const data = await request({
            url: '/api/getInfo',
            method: 'get'
        })
        if (data.code === 200) {
            setStore("userInfo", data.user);
            setStore("permissions", data.permissions);
            await this.getRouters();
            await router.push('/')
        }
    }

    static async getRouters() {
        const {data} = await request({
            url: '/api/getRouters',
            method: 'get'
        });
        setStore('menuData', data.menutreeData);
        handleNestedRoutes(data.routerData)
    }

    static async exitLogin() {
        const data = await request({
            url: '/api/exitLogin',
            method: 'get'
        });
        if (data.code === 200) {
            ElMessage.success({
                message:'登出成功',
                duration:500,
                onClose:()=>{
                    clearAllStore();
                    router.push({path: '/login', replace: true})
                }
            })
        }
    }

}
